package com.stu.util;

import com.stu.dao.impl.ManageDaoImpl;
import com.stu.dao.impl.TeacherDaoImpl;
import com.stu.model.StudentAnswer;
import com.stu.model.Task;
import com.stu.model.User;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import static com.stu.util.JdbcUtils.getConnection;

public class DaoUtile {

    //根据查询语句获取所有的用户
    public static List<User> getAllUser(String sql) throws SQLException {
        Connection conn = JdbcUtils.getConnection();
        PreparedStatement pre = null;
        ResultSet res = null;
        List<User> userList = new ArrayList<>();
        pre = conn.prepareStatement(sql,
                ResultSet.TYPE_SCROLL_INSENSITIVE,
                ResultSet.CONCUR_READ_ONLY);
        res = pre.executeQuery();
        while (res.next()) {
            User user = new User();
            user.setUserAccount(res.getString("userAccount"));
            user.setUserName(res.getString("userName"));
            user.setUserSex(res.getString("userSex"));
            user.setUserBirthday(res.getString("userBirthday"));
            user.setUserIdCard(res.getString("userIdCard"));
            user.setUserPassword(res.getString("userPassword"));
            user.setUserIdentify(Integer.parseInt(res.getString("userIdentify")));
            user.setUserOtherName(res.getString("userOtherName"));
            userList.add(user);
            /*System.out.println(user.getUserIdCard());*/
        }
        return userList;
    }
    //根据查询语句获取所有的任务
    public static List<Task> getTask(String sql) throws SQLException {
        Connection conn = JdbcUtils.getConnection();
        PreparedStatement pre = null;
        ResultSet res = null;
        List<Task> taskList = new ArrayList<>();
        pre = conn.prepareStatement(sql,
                ResultSet.TYPE_SCROLL_INSENSITIVE,
                ResultSet.CONCUR_READ_ONLY);
        res = pre.executeQuery();
        while (res.next()) {
            Task task = new Task();
            task.setTaskAccount(res.getString("taskAccount"));
            task.setTeacherAccount(res.getString("teacherAccount"));
            task.setTaskDate(res.getString("taskDate"));
            task.setTaskName(res.getString("taskName"));
            taskList.add(task);
        }
        return taskList;
    }




    //查询任务的细节即查询学生作答任务的细节
    public static List<StudentAnswer> getTaskDetail(String sql) throws SQLException {
        Connection conn = getConnection();
        PreparedStatement pre = null;
        ResultSet res = null;
        List<StudentAnswer> studentAnswerList = new ArrayList<>();
        pre = conn.prepareStatement(sql,
                ResultSet.TYPE_SCROLL_INSENSITIVE,
                ResultSet.CONCUR_READ_ONLY);
        res = pre.executeQuery();
        while (res.next()) {
            StudentAnswer studentAnswer = new StudentAnswer();
            studentAnswer.setTaskAccount(res.getString("taskAccount"));
            studentAnswer.setStudentAccount(res.getString("studentAccount"));
            studentAnswer.setStudentAnswer(res.getString("studentAnswer"));
            studentAnswer.setIsFinish(res.getInt("isFinish"));
            studentAnswer.setScore(res.getInt("score"));
            studentAnswer.setUserName(res.getString("userName"));
            studentAnswer.setTaskName(res.getString("taskName"));
            studentAnswerList.add(studentAnswer);
        }
        return studentAnswerList;
    }
    //获取当前时间用户给老师添加任务时使用
    public static String getDate(){
        Date date=new Date(System.currentTimeMillis());
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return sdf.format(date);

    }

    //随机生成任务号并判断是否可以用
    public  static String getTaskAccount() throws SQLException {
        String taskAccount="";
        TeacherDaoImpl teacherDao=new TeacherDaoImpl();
        while (true){
            for (int i = 0; i < 4; i++) {
                taskAccount=taskAccount+Integer.toString((int)(1+Math.random()*(10-1+1)));
            }
            //将生成的任务号传递给执行查询操作的sql语句的方法
            int num=teacherDao.isExistTaskAccount(taskAccount);
            if(num==0){
                break;
            }else{
                taskAccount="";
            }
        }
        return taskAccount;
    }
    //查询该任务账户是否存在
    public static int isExist(String sql) throws SQLException {
        Connection conn = getConnection();
        PreparedStatement pre = null;
        ResultSet res = null;
       int num=0;
        pre = conn.prepareStatement(sql,
                ResultSet.TYPE_SCROLL_INSENSITIVE,
                ResultSet.CONCUR_READ_ONLY);
        res = pre.executeQuery();
        while (res.next()) {
           num=res.getInt("num");
        }
        return num;
    }

    //随机生成用户账号(与任务号的原理相似)
    public  static String getPersonAccount() throws SQLException {
        String userAccount="";
        ManageDaoImpl manageDao=new ManageDaoImpl();
        while (true){
            for (int i = 0; i < 7; i++) {
                userAccount= userAccount+Integer.toString((int)(1+Math.random()*(10)));
            }
            int num=manageDao.isExistPersonAccount(userAccount);
            if(num==0){
                break;
            }else{
                userAccount="";
            }
        }
        return userAccount;
    }



    //执行非查询操作的语句
 /*   public static void notQuery(String sql, Object... args) throws SQLException {
        try (Connection connection = getConnection();
             PreparedStatement pstmt = connection.prepareStatement(sql)) {
            for (int i = 0; i < args.length; i++) {
                pstmt.setObject(i + 1, args[i]);
            }
            pstmt.executeUpdate();
        }
    }*/
    //执行非查询操作的语句
    public static void notQuery(String sql) throws SQLException {
        Connection conn = JdbcUtils.getConnection();
        try (PreparedStatement pre = conn.prepareStatement(sql)) {
            pre.executeUpdate();
        }
    }
}
